import pyautogui
import time
import pyperclip
from utils.helper import GenerateReply, WaitShortTime, WaitLongTime, WaitStepTime, TheMsgIsReply
from utils.screener import GetScreenRes, MouseMoveToText, MatchTextList
from utils.config import callMeText, postionMeMsgText, sendBtnText, closeWindowText

def openChatWindow():
    print('正在打开聊天窗口')
    WaitShortTime()
    positionSuc = MouseMoveToText(postionMeMsgText)
    if positionSuc == True:
        print('已定位到@我的聊天窗口')
        checkAndReply()
        return
    else:
        print('未定位到@我的聊天窗口')
    
def closeChatWindow():
    res = GetScreenRes()
    postionSuc = MouseMoveToText(closeWindowText)
    if postionSuc == False:
        print('关闭聊天窗口失败！')
        
def checkAndReply():
    res = MouseMoveToText(postionMeMsgText)
    list = MatchTextList(postionMeMsgText)
    for item in list:
        passed = TheMsgIsReply(item['detected_text'])
        if passed == False:
            ReplyChat(item)
            WaitShortTime()
    # pyautogui.press('enter')
    time.sleep(1)
    closeChatWindow()
def main():
    anyoneCallMe = MouseMoveToText(callMeText)
    if anyoneCallMe:
        openChatWindow()
    else:
        print('等待@中')
        
def ReplyChat(msgItem):
    center_x = msgItem['center_x']
    center_y = msgItem['center_y']
    pyautogui.moveTo(center_x, center_y, duration=1) #移动到消息位置
    pyautogui.rightClick() #唤出微信消息右键菜单
    pyautogui.PAUSE = 0.1
    pyautogui.move(10, 20, duration=0.5)  # 移动到“复制”大概位置
    pyautogui.leftClick() #点击复制按钮
    pyautogui.PAUSE = 0.1
    clipboard_content = pyperclip.paste()
    print(f'获取到剪切板内容：{clipboard_content}')
    gotReply = GenerateReply(clipboard_content.replace(postionMeMsgText, ''));  # 生成回复内容
    pyautogui.moveTo(center_x, center_y, duration=0.1)  #移动到消息位置
    pyautogui.PAUSE = 0.1
    pyautogui.rightClick()
    pyautogui.move(10, 180, duration=1) #移动到“引用”位置
    pyautogui.PAUSE = 0.2
    pyautogui.leftClick() #按下引用
    pyperclip.copy(gotReply)
    print(f'复制回复内容：{gotReply}')
    pyautogui.PAUSE = 1.5
    pyautogui.hotkey('ctrl', 'v')
    print('已粘贴回复内容')

while True:
    checkAndReply()